2020-2021 Sunseeker Telemetry and Lighting System
eusci_a_uart.c
Go to the documentation of this file.
1 //*****************************************************************************
2 //
3 // eusci_a_uart.c - Driver for the eusci_a_uart Module.
4 //
5 //*****************************************************************************
6 
7 //*****************************************************************************
8 //
11 //
12 //*****************************************************************************
13 
14 #include "inc/hw_memmap.h"
15 
16 #ifdef __MSP430_HAS_EUSCI_Ax__
17 #include "eusci_a_uart.h"
18 
19 #include <assert.h>
20 
21 bool EUSCI_A_UART_init(uint16_t baseAddress, EUSCI_A_UART_initParam *param)
22 {
23  bool retVal = STATUS_SUCCESS;
24 
25  //Disable the USCI Module
26  HWREG16(baseAddress + OFS_UCAxCTLW0) |= UCSWRST;
27 
28  //Clock source select
29  HWREG16(baseAddress + OFS_UCAxCTLW0) &= ~UCSSEL_3;
30  HWREG16(baseAddress + OFS_UCAxCTLW0) |= (uint16_t)param->selectClockSource;
31 
32  //MSB, LSB select
33  HWREG16(baseAddress + OFS_UCAxCTLW0) &= ~UCMSB;
34  HWREG16(baseAddress + OFS_UCAxCTLW0) |= param->msborLsbFirst;
35 
36  //UCSPB = 0(1 stop bit) OR 1(2 stop bits)
37  HWREG16(baseAddress + OFS_UCAxCTLW0) &= ~UCSPB;
38  HWREG16(baseAddress + OFS_UCAxCTLW0) |= param->numberofStopBits;
39 
40  //Parity
41  switch (param->parity){
42  case EUSCI_A_UART_NO_PARITY:
43  //No Parity
44  HWREG16(baseAddress + OFS_UCAxCTLW0) &= ~UCPEN;
45  break;
46  case EUSCI_A_UART_ODD_PARITY:
47  //Odd Parity
48  HWREG16(baseAddress + OFS_UCAxCTLW0) |= UCPEN;
49  HWREG16(baseAddress + OFS_UCAxCTLW0) &= ~UCPAR;
50  break;
51  case EUSCI_A_UART_EVEN_PARITY:
52  //Even Parity
53  HWREG16(baseAddress + OFS_UCAxCTLW0) |= UCPEN;
54  HWREG16(baseAddress + OFS_UCAxCTLW0) |= UCPAR;
55  break;
56  }
57 
58  //BaudRate Control Register
59  HWREG16(baseAddress + OFS_UCAxBRW ) = param->clockPrescalar;
60  //Modulation Control Register
61  HWREG16(baseAddress + OFS_UCAxMCTLW) = ((param->secondModReg <<8)
62  + (param->firstModReg <<4) + param->overSampling );
63 
64  //Asynchronous mode & 8 bit character select & clear mode
65  HWREG16(baseAddress + OFS_UCAxCTLW0) &= ~(UCSYNC +
66  UC7BIT +
67  UCMODE_3
68  );
69 
70  //Configure UART mode.
71  HWREG16(baseAddress + OFS_UCAxCTLW0) |= param->uartMode ;
72 
73  //Reset UCRXIE, UCBRKIE, UCDORM, UCTXADDR, UCTXBRK
74  HWREG16(baseAddress + OFS_UCAxCTLW0) &= ~(UCRXEIE + UCBRKIE + UCDORM +
75  UCTXADDR + UCTXBRK
76  );
77  return (retVal);
78 }
79 
80 void EUSCI_A_UART_transmitData ( uint16_t baseAddress,
81  uint8_t transmitData
82  )
83 {
84  //If interrupts are not used, poll for flags
85  if (!(HWREG16(baseAddress + OFS_UCAxIE) & UCTXIE)){
86  //Poll for transmit interrupt flag
87  while (!(HWREG16(baseAddress + OFS_UCAxIFG) & UCTXIFG));
88  }
89 
90  HWREG16(baseAddress + OFS_UCAxTXBUF) = transmitData;
91 }
92 
93 uint8_t EUSCI_A_UART_receiveData (uint16_t baseAddress)
94 {
95  //If interrupts are not used, poll for flags
96  if (!(HWREG16(baseAddress + OFS_UCAxIE) & UCRXIE)){
97  //Poll for receive interrupt flag
98  while (!(HWREG16(baseAddress + OFS_UCAxIFG) & UCRXIFG));
99  }
100 
101  return ( HWREG16(baseAddress + OFS_UCAxRXBUF)) ;
102 }
103 
104 void EUSCI_A_UART_enableInterrupt (uint16_t baseAddress,
105  uint8_t mask
106  )
107 {
108  uint8_t locMask;
109 
110  locMask = (mask & (EUSCI_A_UART_RECEIVE_INTERRUPT
111  | EUSCI_A_UART_TRANSMIT_INTERRUPT
112  | EUSCI_A_UART_STARTBIT_INTERRUPT
113  | EUSCI_A_UART_TRANSMIT_COMPLETE_INTERRUPT));
114 
115  HWREG16(baseAddress + OFS_UCAxIE) |= (uint16_t)locMask;
116 
117  locMask = (mask & (EUSCI_A_UART_RECEIVE_ERRONEOUSCHAR_INTERRUPT
118  | EUSCI_A_UART_BREAKCHAR_INTERRUPT));
119  HWREG16(baseAddress + OFS_UCAxCTLW0) |= (uint16_t)locMask;
120 }
121 
122 void EUSCI_A_UART_disableInterrupt (uint16_t baseAddress,
123  uint8_t mask
124  )
125 {
126  uint8_t locMask;
127 
128  locMask = (mask & (EUSCI_A_UART_RECEIVE_INTERRUPT
129  | EUSCI_A_UART_TRANSMIT_INTERRUPT
130  | EUSCI_A_UART_STARTBIT_INTERRUPT
131  | EUSCI_A_UART_TRANSMIT_COMPLETE_INTERRUPT));
132  HWREG16(baseAddress + OFS_UCAxIE) &= (uint16_t)~locMask;
133 
134  locMask = (mask & (EUSCI_A_UART_RECEIVE_ERRONEOUSCHAR_INTERRUPT
135  | EUSCI_A_UART_BREAKCHAR_INTERRUPT));
136  HWREG16(baseAddress + OFS_UCAxCTLW0) &= (uint16_t)~locMask;
137 }
138 
139 uint8_t EUSCI_A_UART_getInterruptStatus (uint16_t baseAddress,
140  uint8_t mask)
141 {
142  return ( HWREG16(baseAddress + OFS_UCAxIFG) & mask );
143 }
144 
145 void EUSCI_A_UART_clearInterrupt (uint16_t baseAddress, uint16_t mask)
146 {
147  //Clear the UART interrupt source.
148  HWREG16(baseAddress + OFS_UCAxIFG) &= ~(mask);
149 }
150 
151 void EUSCI_A_UART_enable (uint16_t baseAddress)
152 {
153  //Reset the UCSWRST bit to enable the USCI Module
154  HWREG16(baseAddress + OFS_UCAxCTLW0) &= ~(UCSWRST);
155 }
156 
157 void EUSCI_A_UART_disable (uint16_t baseAddress)
158 {
159  //Set the UCSWRST bit to disable the USCI Module
160  HWREG16(baseAddress + OFS_UCAxCTLW0) |= UCSWRST;
161 }
162 
163 uint8_t EUSCI_A_UART_queryStatusFlags (uint16_t baseAddress,
164  uint8_t mask)
165 {
166  return ( HWREG16(baseAddress + OFS_UCAxSTATW) & mask );
167 }
168 
169 void EUSCI_A_UART_setDormant (uint16_t baseAddress)
170 {
171  HWREG16(baseAddress + OFS_UCAxCTLW0) |= UCDORM;
172 }
173 
174 void EUSCI_A_UART_resetDormant (uint16_t baseAddress)
175 {
176  HWREG16(baseAddress + OFS_UCAxCTLW0) &= ~UCDORM;
177 }
178 
179 void EUSCI_A_UART_transmitAddress (uint16_t baseAddress,
180  uint8_t transmitAddress)
181 {
182  //Set UCTXADDR bit
183  HWREG16(baseAddress + OFS_UCAxCTLW0) |= UCTXADDR;
184 
185  //Place next byte to be sent into the transmit buffer
186  HWREG16(baseAddress + OFS_UCAxTXBUF) = transmitAddress;
187 }
188 
189 void EUSCI_A_UART_transmitBreak (uint16_t baseAddress)
190 {
191  //Set UCTXADDR bit
192  HWREG16(baseAddress + OFS_UCAxCTLW0) |= UCTXBRK;
193 
194  //If current mode is automatic baud-rate detection
195  if (EUSCI_A_UART_AUTOMATIC_BAUDRATE_DETECTION_MODE ==
196  (HWREG16(baseAddress + OFS_UCAxCTLW0) &
197  EUSCI_A_UART_AUTOMATIC_BAUDRATE_DETECTION_MODE)){
198  HWREG16(baseAddress + OFS_UCAxTXBUF) = EUSCI_A_UART_AUTOMATICBAUDRATE_SYNC;
199  } else {
200  HWREG16(baseAddress + OFS_UCAxTXBUF) = DEFAULT_SYNC;
201  }
202 
203  //If interrupts are not used, poll for flags
204  if (!(HWREG16(baseAddress + OFS_UCAxIE) & UCTXIE)){
205  //Poll for transmit interrupt flag
206  while (!(HWREG16(baseAddress + OFS_UCAxIFG) & UCTXIFG));
207  }
208 }
209 
210 uint32_t EUSCI_A_UART_getReceiveBufferAddress (uint16_t baseAddress)
211 {
212  return ( baseAddress + OFS_UCAxRXBUF );
213 }
214 
215 uint32_t EUSCI_A_UART_getTransmitBufferAddress (uint16_t baseAddress)
216 {
217  return ( baseAddress + OFS_UCAxTXBUF );
218 }
219 
220 void EUSCI_A_UART_selectDeglitchTime(uint16_t baseAddress,
221  uint16_t deglitchTime
222  )
223 {
224  HWREG16(baseAddress + OFS_UCAxCTLW1) &= ~(UCGLIT1 + UCGLIT0);
225 
226  HWREG16(baseAddress + OFS_UCAxCTLW1) |= deglitchTime;
227 }
228 
229 
230 #endif
231 //*****************************************************************************
232 //
235 //
236 //*****************************************************************************
uint8_t transmitData
MPU_initThreeSegmentsParam param
#define HWREG16(x)
Definition: hw_memmap.h:39
#define STATUS_SUCCESS
Definition: hw_memmap.h:22